package org.cqut.system.resource.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.cqut.framework.web.response.ResultInfo;
import org.cqut.framework.web.response.Status;
import org.cqut.system.resource.domain.Resource;
import org.cqut.system.resource.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author XiAoXu
 * @date 2021/11/4 13:17
 * @description
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    @GetMapping("/list")
    public ResultInfo list(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam("condition") String condition) {
        IPage<Resource> list = resourceService.getResourceList(page, limit, condition);
        if (Objects.nonNull(list)) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, list);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @DeleteMapping("/{resourceId}")
    public ResultInfo delete(@PathVariable String resourceId) {
        boolean flag = resourceService.removeResourceById(resourceId);
        if (flag) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @PostMapping("/rename")
    public ResultInfo rename(@RequestBody Resource resource){
        boolean flag = resourceService.rename(resource);
        if (flag) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }
}
